﻿namespace PlaylistCopy.Services.Impl
{
    using System.Diagnostics;
    using CuttingEdge.Conditions;
    using PlaylistCopy.Components;
    using System.Collections.Generic;
    using System.Linq;
    using PlaylistCopy.Contract;

    internal class PlaylistService : IPlaylistService
    {
        private readonly IPlaylistImporter playlistImporter;

        private readonly IFileService fileService;

        public PlaylistService(IPlaylistImporter playlistImporter, IFileService fileService)
        {
            this.playlistImporter = playlistImporter;
            this.fileService = fileService;
        }

        public IEnumerable<string> GetAllExtensions()
        {
            return this.playlistImporter.Query().Select(playlist => playlist.PlaylistExtension).ToList();
        }

        public IPlaylist GetPlaylistForFilePath(string filePath)
        {
            Condition.Requires(filePath, "filePath").IsNotNullOrWhiteSpace();

            Debug.Assert(filePath != null, "filePath != null");
            var fileExt = this.fileService.FileExtension(filePath).TrimStart('.');

            return this.playlistImporter.Query().FirstOrDefault(playlist => playlist.PlaylistExtension == fileExt);
        }
    }
}
